﻿#include "videolayoutconfig.h"
#include "ui_videolayoutconfig.h"
#include "characterset.h"

VideoLayoutConfig::VideoLayoutConfig(QWidget *parent) :
    QWidget(parent), ui(new Ui::VideoLayoutConfig),
    mode(0), row(0), column(0), videoCount(0)
{
    ui->setupUi(this);
    on_radio_default_toggled(ui->radio_default->isChecked());
}

VideoLayoutConfig::~VideoLayoutConfig()
{
    delete ui;
}

void VideoLayoutConfig::initConfig(qint8 mode, int row, int column, int videoCount)
{
    if(mode == 0)
        ui->radio_default->setChecked(true);
    else if(mode == 1)
        ui->radio_grid->setChecked(true);

    ui->group_params->setTitle(QObject::trECSUTF8("视频数量: ")+QString::number(videoCount));

    this->mode = mode;
    this->row = row;
    this->column = column;
    this->videoCount = videoCount;

    if(row > 1)
        ui->spin_row->setValue(row);
    else
        on_spin_row_valueChanged(ui->spin_row->value());
    if(column > 1)
        ui->spin_column->setValue(column);
    else
        on_spin_column_valueChanged(ui->spin_column->value());
}

void VideoLayoutConfig::getConfig(qint8 &mode, int &row, int &column)
{
    mode = this->mode;
    row = this->row;
    column = this->column;
}

void VideoLayoutConfig::on_radio_default_toggled(bool checked)
{
    ui->group_params->setEnabled(!checked);
}

void VideoLayoutConfig::on_spin_column_valueChanged(int columnCount)
{
    if(columnCount == 0 || videoCount == 0)
        return;
    int rowCount = (videoCount + columnCount - 1) / columnCount;
    ui->spin_row->setMaximum(rowCount);
}

void VideoLayoutConfig::on_spin_row_valueChanged(int rowCount)
{
    if(rowCount == 0 || videoCount == 0)
        return;
    int columnCount = (videoCount + rowCount - 1) / rowCount;
    ui->spin_column->setMaximum(columnCount);
}

void VideoLayoutConfig::on_buttonBox_accepted()
{
    bool changed = false;
    if(ui->radio_default->isChecked())
    {
        if(mode != 0)
        {
            mode = 0;
            changed = true;
        }
    }
    else if(ui->radio_grid->isChecked())
    {
        if(mode != 1)
        {
            mode = 1;
            changed = true;
        }
    }
    if(ui->group_params->isEnabled())
    {
        int rowValue = ui->spin_row->value();
        int columnValue = ui->spin_column->value();
        if(row != rowValue)
        {
            row = rowValue;
            changed = true;
        }
        if(column != columnValue)
        {
            column = columnValue;
            changed = true;
        }
    }
    emit Ended(changed);
}

void VideoLayoutConfig::on_buttonBox_rejected()
{
    emit Ended(false);
}
